package com.qf.dao;

import com.qf.pojo.Contractor;
import com.qf.pojo.Developer;
import com.qf.pojo.Project;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * @version 1.0
 * 公众号：Java架构栈
 * @Author: 卓不凡
 */
public interface ProjectDao {
    //查询工程表需要将对应开发商信息查出来
    //先用注解实现   status代表状态值  1
    @Select("select  * from project  where status = 1")
    @Results(id="projectMap",value={
            @Result(column = "pid",property = "pid"),
            @Result(column = "pname",property = "pname"),
            @Result(column = "starttime",property = "starttime"),
            @Result(column = "endtime",property = "endtime"),
            @Result(column = "did",property = "did"),
            @Result(column = "cid",property = "cid"),
            @Result(column = "updatetime",property = "updatetime"),
            @Result(column = "did",property = "developer",javaType = Developer.class,
            one=@One(select="com.qf.dao.DevelopDao.findDevelopeByDid",fetchType = FetchType.EAGER)),
            @Result(column = "cid",property = "contractor",javaType = Contractor.class,

                    one = @One(select = "com.qf.dao.ConstractorDao.findBycID",fetchType = FetchType.EAGER)
            ),
            @Result(column = "pid",property = "stages",

                    many = @Many(select = "com.qf.dao.StageDao.findStagesByPid",fetchType = FetchType.EAGER)
            ),
            @Result(column = "pid",property = "subitems",

                    many = @Many(select = "com.qf.dao.SubitemDao.findSubitems",fetchType = FetchType.EAGER)
            ),
            @Result(column = "pid",property = "branchs",

                    many = @Many(select = "com.qf.dao.BranchDao.findBypid",fetchType = FetchType.EAGER)
            )

    })
    List<Project>  findProjects();
    @ResultMap("projectMap")
    @Select("select * from project where pid=#{pid}")
    Project findProjectByPid(int pid);

    //新增project
    @Insert("<script> insert into project (\n" +
            "            <trim suffixOverrides=\",\">\n" +
            "                <if test=\"pname!=null and pname!=''\">\n" +
            "                    pname,\n" +
            "                </if>\n" +
            "                <if test=\"starttime!=null\">\n" +
            "                    starttime,\n" +
            "                </if>\n" +
            "                <if test=\"endtime!=null\">\n" +
            "                    endtime,\n" +
            "                </if>\n" +
            "                <if test=\"did!=null and did!=''\">\n" +
            "                    did,\n" +
            "                </if>\n" +
            "                <if test=\"cid!=null and cid!=''\">\n" +
            "                    cid,\n" +
            "                </if>\n" +
            "            </trim>\n" +
            "        )\n" +
            "        values (\n" +
            "            <trim suffixOverrides=\",\">\n" +
            "                <if test=\"pname!=null and pname!=''\">\n" +
            "                    #{pname},\n" +
            "                </if>\n" +
            "                <if test=\"starttime!=null\">\n" +
            "                    #{starttime},\n" +
            "                </if>\n" +
            "                <if test=\"endtime!=null\">\n" +
            "                    #{endtime},\n" +
            "                </if>\n" +
            "                <if test=\"did!=null and did!=''\">\n" +
            "                    #{did},\n" +
            "                </if>\n" +
            "                <if test=\"cid!=null and cid!=''\">\n" +
            "                    #{cid},\n" +
            "                </if>\n" +
            "            </trim>\n" +
            "        )</script>")
    void  addProject(Project project);
}
